package com.yy.db;

import com.yy.pojo.User;

public class UserDBUtil extends DBUtil {
User user1=null;
    public User login(User user) {
        try {
            getConn();
            String sql = "select * from `user` where username=? and password=?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, user.getUsername());
            ps.setObject(2, user.getPassword());
            rs = ps.executeQuery();
            if (rs.next()) {
                user1 = new User(rs.getString(2),
                        rs.getString(3),
                        rs.getString(4));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConn();
        }
        return user1;
    }

    public boolean nicknameExist(String nickname) {
        try {
            getConn();
            String sql="select nickname from `user` where nickname=?";
            ps=conn.prepareStatement(sql);
            ps.setObject(1, nickname);
            rs=ps.executeQuery();
            if(rs.next()){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeConn();
        }
        return false;
    }

    public boolean addUser(User user) {
        int i=0;
        try {
            getConn();
            String sql="insert into `user` (username,password,nickname) values (?,?,?);";
            ps=conn.prepareStatement(sql);
            ps.setObject(1, user.getUsername());
            ps.setObject(2, user.getPassword());
            ps.setObject(3, user.getNickname());
            i=ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            closeConn();
        }
        return i>0;
    }
}
